package com.nnfe.fyd.feature.inquiryreport;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nnfe.fyd.BusKey;
import com.nnfe.fyd.R;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.DialogUtil;
import com.nnfe.fyd.utils.LogUtil;
import com.nnfe.fyd.utils.ToastUtil;
import com.nnfe.fyd.utils.common.DecimalTextWatcher;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/3/21.
 * 报单
 */

public class InquiryReportFragment extends ActionBarFragment {
    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.amount)
    EditText amount;
    @Bind(R.id.baodan)
    Button baodan;
    @Bind(R.id.dingdanhao)
    TextView dingdanhao;

    // 最大额度
    private String max;
    // 订单号
    private String order_id;


    @Override
    public Object getTitleString() {
        return "报单";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_inquiry_report;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {
        amount.addTextChangedListener(new DecimalTextWatcher());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        order_id = getArguments().getString("id");
        LogUtil.e("dada", "id:" + order_id);
        dingdanhao.append("" + order_id);
    }

    @OnClick(R.id.baodan)
    public void onClick() {
        if(checkCanReport()){
            baodan.setEnabled(false);
            doReport();
        }
    }

    /**
     * 去报单
     */
    private void doReport() {
        DialogUtil.showLoadingDialog(mContext,"提交报单中...");
        service.inquiryReport(order_id, name.getText().toString(), amount.getText().toString()).enqueue(new CallbackImpl() {
            @Override
            public void responseSuccess(Call call, Response response) {
                mContext.finish();
                ToastUtil.showLongToast(mContext, "提交成功");
                EventBus.getDefault().post(BusKey.POST_inquiryReport);
            }

            @Override
            protected void onRequestFinish() {
                super.onRequestFinish();
                baodan.setEnabled(true);
                DialogUtil.dismissLoadingDialog(mContext);
            }

        });
    }

    /**
     * 检测是否符合报单条件
     * @return
     */
    private boolean checkCanReport() {
        // 可贷款的最大额
        max = getArguments().getString("num");

        if (TextUtils.isEmpty(name.getText().toString())) {
            ToastUtil.showToast("请输入正确的姓名");
            return false;
        }

        if (TextUtils.isEmpty(amount.getText())) {
            ToastUtil.showToast("输入贷款金额");
            return false;
        }
        try {
            // 输入金额
            double num1 = Double.parseDouble(amount.getText().toString());
            // 最大限制金额
            double num2 = Double.parseDouble(max);
            if (num1 == 0) {
                ToastUtil.showToast("贷款金额输入不能为0！");
                return false;
            }

            if (num1 > num2) {
                ToastUtil.showToast("贷款金额不能超过贷款额度!");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }


}
